package com.cf.service.impl;

import com.cf.dao.*;
import com.cf.entity.Item;
import com.cf.entity.ItemMan;
import com.cf.entity.SupportOrder;
import com.cf.entity.User;
import com.cf.service.SupportOrderService;
import com.cf.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SupportOrderImpl implements SupportOrderService {
    @Autowired
    private SupportOrderDao supportOrderDao;
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private FocusDao focusDao;
    @Autowired
    private ItemManDao itemManDao;

    @Override
    public int insertSU(SupportOrder supportOrder) {
        supportOrder.setOrderuuid(UUIDUtil.getUUID());
        //添加支持订单
        int i1 = supportOrderDao.insertSU(supportOrder);
        if(i1!=0){
            //查找当前项目的支持人数
            int havenman = supportOrderDao.selectSupportpeo(supportOrder.getIid());
            //更新项目的支持人数
            int i = itemDao.updateHaveMan(havenman, supportOrder.getAllmoney(),supportOrder.getIid());
            return i;
        }else {
            return i1;
        }

    }

    @Override
    public PageInfo<SupportOrder> SUByuserid(int pageNum, int pageSize,long userid) {
        PageHelper.startPage(pageNum,pageSize);
        List<SupportOrder> list = supportOrderDao.SUByuserid(userid);
        for (SupportOrder supportOrder : list) {
            Item item = itemDao.selectItemByIid(supportOrder.getIid());
            double  percentage =  item.getNmoney()/item.getAmoney()*100;
            long round = Math.round(percentage);
            item.setPercentage(round);

            //将发起人封装
            ItemMan itemMan = itemManDao.selectItemManByIid(supportOrder.getIid());
            item.setItemMan(itemMan);

            //查询项目总关注条数
            int gzcount = focusDao.selectCountByitemid(item.getIid());
            item.setGzcount(gzcount);

            //计算天数
            try {
                if (item.getIstart()==null||item.getIend()==null){
                    item.setDaybetween(0);
                }else {
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    Date stime =sdf.parse(sdf.format(new Date()));
                    Date etime =sdf.parse(sdf.format(item.getIend()));
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(stime);
                    long time1 = cal.getTimeInMillis();
                    cal.setTime(etime);
                    long time2 = cal.getTimeInMillis();
                    long between_days=(time2-time1)/(1000*3600*24);
                    item.setDaybetween(between_days);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            supportOrder.setItems(item);
        }

        PageInfo<SupportOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public SupportOrder selectBySupport(long orderid) {
        return supportOrderDao.selectBySupport(orderid);
    }

    @Override
    public int delSupportpeo(SupportOrder supportOrder) {
        supportOrderDao.delSupportpeo(supportOrder.getOrderuuid());
        //查找当前项目的支持人数
        int havenman = supportOrderDao.selectSupportpeo(supportOrder.getIid());
        //更新项目的支持人数
        int i = itemDao.updateJHaveMan(havenman, supportOrder.getAllmoney(),supportOrder.getIid());

        return i;
    }
    @Override
    public int updateStatus(String orderuuid) {
        return supportOrderDao.updateStatus(orderuuid);
    }
}
